export const useDigital = () => {
  /**
   * @description : 格式化金额
   * @param        {string | number} price 金额
   * @param        {number} num 需要保留的小数点
   * @return       {string}
   */
  const formatPrice = (price, num = 2) => {
      let isNegative = Number(price)  < 0;
    if (price === undefined || price === null || price === '') return '-';
    num = num > 0 && num <= 20 ? num : 2;
    price = Math.abs(price);
    price = parseFloat((price + '').replace(/[^\d\\.-]/g, '')).toFixed(num) + '';
    var l = price.split('.')[0].split('').reverse(),
      r = price.split('.')[1];
    let t = '';
    for (let i = 0; i < l.length; i++) {
      t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? ',' : '');
    }
    return isNegative? '-' + t.split('').reverse().join('') + '.' + r : t.split('').reverse().join('') + '.' + r;
  };

  /**
   * @description : render函数渲染千分符金额
   * @param        {function} render函数
   * @param        {object} 行，列
   * @return       {html}
   */
  const setAmount = (h, { row, column }) => {
    const { key } = column;
    return h('span', null, formatPrice(row[key]));
  };

  return {
    formatPrice,
    setAmount,
  };
};
